package com.wang.bootbuilder.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/*
配置mybatis-plus自动填充的配置类
 */
@Component
public class AutoFillHandler implements MetaObjectHandler {

    /*
    登录时自动填充
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        // 填充创建者
        this.fillStrategy(metaObject, "createBy", "admin");
        // 填充更新者
        this.fillStrategy(metaObject, "updateBy", "admin");
        // 填充创建时间
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        // 填充更新时间
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        // 填充逻辑删除
        this.strictInsertFill(metaObject, "deleted", Integer.class, 0);
        // 填充乐观锁
        this.strictInsertFill(metaObject, "version", Integer.class, 1);
    }

    /*
    更新时自动填充
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        // 填充更新者
        this.fillStrategy(metaObject, "updateBy", "admin");
        // 填充更新时间
        this.fillStrategy(metaObject, "updateTime", LocalDateTime.now());
    }

}
